/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */
/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */

import { adminRoutes } from "@/router";
import { CategoryType } from "@/types/pro";
import { IMenuType } from "@/types/router";

export function dataTree(data: CategoryType[]) {
  const root: any = {};
  for (const item of data.sort((a, b) => a.level - b.level)) {
    const parent =
      (item.parent_id && root[item.parent_id]) ||
      (item.grandparent_id && root[item.grandparent_id]) ||
      root;
    parent.children = parent.children || [];
    parent.children.push(item);
    root[item.objectId] = item;
  }
  console.log(root);
  return root.children;
}

// path--label菜单映射

// 动态属性
interface BreadMapType {
  [key: string]: string;
}
export function breadMap() {
  let breadcrumbNameMap: BreadMapType = {};
  // let breadcrumbNameMap: Record<string, string> = {};

  function getBreadMapData(data: IMenuType[]) {
    data.forEach((item) => {
      breadcrumbNameMap[item.key] = item.label;
      if (item.children) {
        getBreadMapData(item.children);
      }
    });
  }

  getBreadMapData(adminRoutes);
  return breadcrumbNameMap;
}
